import Vue from 'vue'
import App from './App.vue'
import router from './router.js'
import jv from './api/enums.js'
import axios from 'axios'
// import ElementUI from 'element-ui'
import ElementUIExt from 'element-ui-ext'
// import ElementUIExt_Css from 'element-ui-ext/lib/element-ui-ext.css'
import './element-variables.scss'

(function (jv) {
    window.jv = jv;

    jv.initVue({vue: Vue, axios: axios, router: router});
    Vue.config.productionTip = true;

    jv.reset = function () {
    };

    jv.debugger = function (v) {
        console.log(v);
    };

    window.addEventListener('message', (e) => {
        if (e.source.parent != window) return;
        //两个属性： event,arguments
        if (e.data.event == "error") {
            jv.error.apply(jv, e.data.arguments)
        }
    }, false);


    jv.system = "open"

    Object.defineProperty(jv, "token", {
        get() {
            return window.localStorage.getItem(jv.system + '-token') || "";
        },
        set(value) {
            window.localStorage.setItem(jv.system + '-token', value || "");
        },
        enumerable: false
    });
    axios.interceptors.request.use(config => {
        var token = jv.token;
        if (token) {
            config.headers.post["token"] = token;
            config.headers.get["token"] = token;
        }
        return config;
    }, error => {
        return Promise.reject(error);
    });


    axios.interceptors.response.use(response => {
        try {
            var token = response.headers["token"];
            if (token) {
                jv.token = token;
            }
        } catch (e) {
            console.error("axios.interceptors.response:" + e.message)
            return Promise.reject(e);
        }
        return response;
    }, error => {
        var resp = error.response;
        if (resp) {
            var status = resp.status;
            if (status == 401) {
                jv.main.loginFromVisible = true;
                return Promise.reject(error);
            }
        }
        return Promise.reject(error);
    });

    // Vue.use(VueQuillEditor);
    /*Vue.config.productionTip = false;*/
    // Vue.use(ElementUI);
    Vue.use(ElementUIExt);



    window.onerror = function (strMsg, url, line) {
        if (strMsg) {
            console.log(strMsg);
        }
    };


})(jv);


(function (jv) {


    // Vue.mixin({
    //     mounted() {
    //         if (this.$el) {
    //             var tagName = this.$options._componentTag;
    //             console.log(tagName);
    //         }
    //     }
    // });


    var vue = new Vue({
        router,
        /*        watch: {
                    '$route'(to, from) {
                        jv.loading = false;
                    }
                },*/
        render: h => h(App),
        mounted() {
            document.dispatchEvent(new Event('render-event'))
        }
    }).$mount('#app')

    vue.$nextTick(it => {
        // jv.main.loadData();
        document.documentElement.classList.add("loaded");
    });

})(jv);
